package com.atguigu.controller;

import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @title：
 * @author：Yu
 * @create：2023-02-08 23:12
 * @description：
 */
@Controller
@RequestMapping("/community")
public class CommunityController extends BaseController {
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private DictService dictService;

    @RequestMapping()
    public String findPage(Map map, HttpServletRequest request) {
        Map<String, Object> filters = getFilters(request);
        map.put("filters", filters);
        PageInfo<Community> page = communityService.findPage(filters);
        map.put("page", page);
        List<Dict> provice = dictService.findProvice(100000L);
        System.out.println("1");
        map.put("provice", provice);
        List<Dict> byId = dictService.findAll();
        map.put("areaList", byId);
        return "community/index";
    }

    @RequestMapping("/create")
    public String create(Map map) {
        List<Dict> provice = dictService.findProvice(100000L);
        map.put("provice", provice);
        List<Dict> byId = dictService.findAll();
        map.put("areaList", byId);
        return "community/create";
    }

    @RequestMapping("/save")
    public String insert(Community community) {
        communityService.insert(community);
        return "common/success";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Map map) {
        Community byId = communityService.findById(id);
        map.put("community", byId);
        List<Dict> provice = dictService.findProvice(100000L);
        map.put("provice", provice);
        List<Dict> area = dictService.findAll();
        map.put("areaList", area);
        return "community/edit";
    }

    @RequestMapping("/update")
    public String update(Community community) {
        communityService.update(community);
        return "common/success";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        communityService.delete(id);
        return "common/success";
    }
}
